﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace YTSK.cSK
{
    class cDllExt
    {
        #region 获取所有控件
        /// <summary>
        /// 获取所有控件
        /// </summary>
        /// <param name="hWndParent"></param>
        /// <returns></returns>
        public static List<Model.WinProperty> GetAllChild(IntPtr hWndParent)
        {
            var tmp = new cDllExt_GetAllControl();
            cDll.EnumChildWindows(hWndParent, tmp.EnumAllChildControl, 0);
            var l_con = tmp._lcontrol;
            for(int i=0;i<l_con.Count;i++)
            {
                var con = l_con[i];
                cDllExt.GetWinProperty(con);
            }
            return l_con;
        }

        #endregion

        #region 获取所有控件-子过程
        class cDllExt_GetAllControl
        {
            public List<Model.WinProperty> _lcontrol;
            public cDllExt_GetAllControl()
            {
                _lcontrol = new List<Model.WinProperty>();
            }
            public bool EnumAllChildControl(IntPtr hwnd, int lParam)
            {
                var con = new Model.WinProperty();
                con.hwnd = hwnd;
                _lcontrol.Add(con); 
                return true;
            }
        }
        #endregion

        #region 获取控件的所有属性
        /// <summary>
        /// 获取窗口的所有属性
        /// </summary>
        /// <param name="winproperty"></param>
        public static void GetWinProperty(Model.WinProperty winproperty)
        {
            cDll.GetWindowRect(winproperty.hwnd, out winproperty.position);
            //var len= cDll.GetWindowTextLength(winproperty.hwnd);
            var sb = new StringBuilder(200);
            cDll.GetWindowText(winproperty.hwnd, sb, 200);
            winproperty.text = sb.ToString();
            //if (winproperty.text == null || winproperty.text == "")
            //{
            //    const int WM_GETTEXT = 0x000D;
            //    cDll.SendMessage(winproperty.hwnd, WM_GETTEXT, sb.Capacity, sb);
            //    winproperty.text = sb.ToString();
            //}
            sb = new StringBuilder(200);
            cDll.GetClassName(winproperty.hwnd, sb, 200);
            winproperty.classname = sb.ToString();
            winproperty.parent = cDll.GetParent(winproperty.hwnd);
        }
        #endregion

        #region 鼠标双击某个位置
        /// <summary>
        /// 鼠标双击某个位置
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public static void mouseDoubleClick(int x, int y)
        {
            cDll.SetCursorPos(x, y);
            System.Threading.Thread.Sleep(200);
            cDll.mouse_event(MouseEvent.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
            cDll.mouse_event(MouseEvent.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
            System.Threading.Thread.Sleep(300);
            cDll.mouse_event(MouseEvent.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
            cDll.mouse_event(MouseEvent.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
            System.Threading.Thread.Sleep(200);
        }
        #endregion


        #region 鼠标单击某个位置
        /// <summary>
        /// 鼠标单击某个位置
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public static void mouseClick(int x, int y)
        {
            cDll.SetCursorPos(x, y);
            System.Threading.Thread.Sleep(200);
            cDll.mouse_event(MouseEvent.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
            cDll.mouse_event(MouseEvent.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
            //System.Threading.Thread.Sleep(100);
        }
        #endregion

        #region 单击某个键
        public static void KeyPress(Keys key)
        {
            cDll.keybd_event((byte)key, 0, 0, 0);
            cDll.keybd_event((byte)key, 0, 2, 0);
        }

        #endregion



        /// <summary>
        /// 获取宽度缩放百分比
        /// </summary>
        public static float ScaleX
        {
            get
            {
                if (_scalex == 0)
                {
                    IntPtr hdc = cDll.GetDC(IntPtr.Zero);
                    _scalex = (float)cDll.GetDeviceCaps(hdc, DeviceCapsIndex.DESKTOPHORZRES) / (float)cDll.GetDeviceCaps(hdc, DeviceCapsIndex.HORZRES);
                    cDll.ReleaseDC(IntPtr.Zero, hdc);
                }
                return _scalex;
            }
        }
        private static float _scalex = 0;
        private static float _scaley = 0;
        /// <summary>
        /// 获取高度缩放百分比
        /// </summary>
        public static float ScaleY
        {
            get
            {
                if (_scaley == 0)
                {
                    IntPtr hdc = cDll.GetDC(IntPtr.Zero);
                    _scaley = (float)(float)cDll.GetDeviceCaps(hdc, DeviceCapsIndex.DESKTOPVERTRES) / (float)cDll.GetDeviceCaps(hdc, DeviceCapsIndex.VERTRES);
                    cDll.ReleaseDC(IntPtr.Zero, hdc);
                }
                return _scaley;
            }
        }


        /// <summary>
        /// 获取真实设置的桌面分辨率大小
        /// </summary>
        public static Size DESKTOP
        {
            get
            {
                IntPtr hdc = cDll.GetDC(IntPtr.Zero);
                Size size = new Size();
                size.Width = cDll.GetDeviceCaps(hdc, DeviceCapsIndex.DESKTOPHORZRES);
                size.Height = cDll.GetDeviceCaps(hdc, DeviceCapsIndex.DESKTOPVERTRES);
                cDll.ReleaseDC(IntPtr.Zero, hdc);
                return size;
            }
        }


    }
}
